package com.hhl.lamda.reuseastream;

import java.util.Arrays;
import java.util.function.Supplier;
import java.util.stream.Stream;

/**
 * @author HHL
 * @description
 * @date 2018/11/19 - 9:48
 */
public class ReuseAStream {

    public static void main(String[] args) {

        /* java.lang.IllegalStateException: stream has already been operated upon or closed */
//        String[] array = {"a", "b", "c", "d", "e"};
//        Stream<String> stream = Arrays.stream(array);
//
//        // loop a stream
//        stream.forEach(x -> System.out.println(x));
//
//        // reuse it to filter again! throws IllegalStateException
//        long count = stream.filter(x -> "b".equals(x)).count();
//        System.out.println(count);
        /* java.lang.IllegalStateException: stream has already been operated upon or closed */




        String[] array = {"a", "b", "c", "d", "e"};
        Supplier<Stream<String>> streamSupplier = () -> Stream.of(array);

        //get new stream
        streamSupplier.get().forEach(x -> System.out.print(x));
        // abcde
        System.out.println();

        //get another new stream
        long count = streamSupplier.get().filter(x -> "b".equals(x)).count();
        // 1
        System.out.println(count);






    }
}
